package com.example.anni.zhuzhoujia.bogege;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.anni.zhuzhoujia.R;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.ScrollIndicatorView;
import com.shizhefei.view.indicator.slidebar.DrawableBar;
import com.shizhefei.view.indicator.slidebar.ScrollBar;
import com.shizhefei.view.indicator.transition.OnTransitionTextListener;

/**
 * Created by 张钧博 on 2016/6/2.
 * 新闻的大框架
 * 里面又放了一个viewPager
 */
public class NewsFragment extends Fragment {
    private IndicatorViewPager indicatorViewPager;
    private LayoutInflater inflate;
    private ScrollIndicatorView scrollIndicatorView;
    private int unSelectTextColor;
    private ImageButton newsSearch;

    private String[] newstypeNames = {
            "热门",
            "要闻",
            "时尚娱乐",
            "株洲民生",
            "株洲时政",
            "军事历史",
            "微话题",
            "爆料"
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.news_fragment, null);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ViewPager viewPager = (ViewPager) view.findViewById(R.id.moretab_viewPager);
        newsSearch = (ImageButton) view.findViewById(R.id.news_search);
        scrollIndicatorView = (ScrollIndicatorView) view.findViewById(R.id.moretab_indicator);
        scrollIndicatorView.setBackgroundColor(getResources().getColor(R.color.indicator_bac));
        scrollIndicatorView.setScrollBar(new DrawableBar
                (getContext(), R.drawable.round_border_white_selector, ScrollBar.Gravity.CENTENT_BACKGROUND) {
            @Override
            public int getHeight(int tabHeight) {
                return tabHeight - dipToPix(12);
            }

            @Override
            public int getWidth(int tabWidth) {
                return tabWidth - dipToPix(12);
            }
        });
        unSelectTextColor = Color.WHITE;
        // 设置滚动监听
        scrollIndicatorView.setOnTransitionListener(new OnTransitionTextListener().setColor(Color.RED, unSelectTextColor));

        viewPager.setOffscreenPageLimit(2);
        indicatorViewPager = new IndicatorViewPager(scrollIndicatorView, viewPager);
        inflate = LayoutInflater.from(getContext());
        indicatorViewPager.setAdapter(new NewsAdapter(getChildFragmentManager()));
        newsSearch.setOnClickListener(new NewsSearchListener());
    }

    class NewsAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter {

        public NewsAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        @Override
        public int getCount() {
            return newstypeNames.length;
        }

        @Override
        public View getViewForTab(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = (TextView) inflate.inflate(R.layout.tab_top, viewGroup, false);
            }
            TextView textView = (TextView) view;
            textView.setText(newstypeNames[i % newstypeNames.length]);
            int padding = dipToPix(10);
            textView.setPadding(padding, 0, padding, 0);
            return textView;
        }

        @Override
        public Fragment getFragmentForPage(int i) {
            return new HotFragment(i);
        }

        @Override
        public int getItemPosition(Object object) {
            //这是ViewPager适配器的特点,有两个值 POSITION_NONE，POSITION_UNCHANGED，默认就是POSITION_UNCHANGED,
            // 表示数据没变化不用更新.notifyDataChange的时候重新调用getViewForPage
            return PagerAdapter.POSITION_NONE;
        }
    }

    /**
     * 根据dip值转化成px值
     *
     * @param dip
     * @return
     */
    private int dipToPix(float dip) {
        int size = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, getResources().getDisplayMetrics());
        return size;
    }

    /**
     * 进入搜索新闻页
     */
    private class NewsSearchListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), NewsSearchActivity.class);
            startActivity(intent);
        }
    }
}